package com.ljm.boot.seataorderdetails.controller;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ljm.boot.Utils.R;
import com.ljm.boot.seataorderdetails.entity.OrderDetails;
import com.ljm.boot.seataorderdetails.service.OrderDetailsService;

/**
 * 订单表(OrderDetails)表控制层
 *
 * @author LiRui
 * @since 2025-04-07 16:00:11
 */
@RestController
@RequestMapping("orderDetails")
public class OrderDetailsController {

    private static final Logger log = LoggerFactory.getLogger(OrderDetailsController.class);

    /**
     * 服务对象
     */
    @Resource
    private OrderDetailsService orderDetailsService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public OrderDetails selectOne(@PathVariable Serializable id) {
        return this.orderDetailsService.getById(id);
    }

    /**
     * 新增数据
     *
     * @param orderDetails 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody OrderDetails orderDetails) {
        // 打印接收到的 XID
        log.info("OrderDetails 服务接收到的 XID: {}", io.seata.core.context.RootContext.getXID());

        boolean save = this.orderDetailsService.save(orderDetails);
        if (save) {
            throw new RuntimeException("测试分布式事务回滚");
        }
         return R.ok();
    }

    /**
     * 修改数据
     *
     * @param orderDetails 实体对象
     * @return 修改结果
     */
    @PutMapping
    public void update(@RequestBody OrderDetails orderDetails) {
         this.orderDetailsService.updateById(orderDetails);
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public void delete(@RequestParam("idList") List<Long> idList) {
         this.orderDetailsService.removeByIds(idList);
    }
}

